package com.supermap.dialogs;

import com.supermap.Helper;
import com.supermap.analyst.networkanalyst.NetworkBuilder;
import com.supermap.consts.AttrConsts;
import com.supermap.data.*;
import com.supermap.dialogs.advance.CJDialog;

import javax.swing.*;
import java.awt.*;

public class NetworkBuilderDialog extends CJDialog {
    private JComboBox<String> srcDatasourceCmb;
    private JComboBox<String> srcDatasetCmb;
    private JCheckBox autoBreakPointCbx;
    private JCheckBox autoBreakLineCbx;
    private JTextField breakToleranceTxt;
    private JComboBox<String> resultDatasourceCmb;
    private JTextField resultDatasetTxt;
    private JButton fieldSettingBtn;

    private Workspace workspace;
    private String[] pointFieldNames;
    private String[] lineFieldNames;

    public NetworkBuilderDialog(Frame owner, String title, boolean modal, Workspace workspace) {
        super(owner, title, modal, 600, 400, new Object[]{workspace});
    }

    private void loadDatasets(JComboBox<String> srcDatasetCmb, Datasource datasource) {
        srcDatasetCmb.removeAll();
        Datasets datasets = datasource.getDatasets();
        int datasetCount = datasets.getCount();
        for (int i = 0; i < datasetCount; i++) {
            Dataset dataset = datasets.get(i);
            DatasetType datasetType = dataset.getType();
            if (datasetType == DatasetType.LINE || datasetType == DatasetType.NETWORK) {
                srcDatasetCmb.addItem(dataset.getName());
            }
        }
    }

    @Override
    protected void initializeArgs(Object[] args) {
        this.workspace = (Workspace) args[0];
    }

    @Override
    protected void initializeCore() {
        setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        super.confirmBtn.setText("构建");

        JPanel centerPanel = new JPanel();
        centerPanel.setLayout(new GridBagLayout());
        add(centerPanel, BorderLayout.CENTER);

        JPanel srcDataPanel = new JPanel();
        srcDataPanel.setBorder(BorderFactory.createTitledBorder("源数据"));
        srcDataPanel.setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.fill = GridBagConstraints.BOTH;
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.gridwidth = 1;
        gbc.gridheight = 1;
        gbc.weightx = 1;
        gbc.weighty = 1;
        gbc.insets = Helper.DefaultInsets;
        centerPanel.add(srcDataPanel, gbc);

        JLabel srcDatasourceLbl = new JLabel("数据源: ");
        gbc = new GridBagConstraints();
        gbc.fill = GridBagConstraints.BOTH;
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.gridwidth = 1;
        gbc.gridheight = 1;
        gbc.weightx = 1;
        gbc.weighty = 1;
        gbc.insets = Helper.DefaultInsets;
        srcDataPanel.add(srcDatasourceLbl, gbc);

        srcDatasourceCmb = new JComboBox<>();
        Datasources datasources = workspace.getDatasources();
        int datasourcesCount = datasources.getCount();
        if (datasourcesCount > 0) {
            for (int i = 0; i < datasourcesCount; i++) {
                Datasource datasource = datasources.get(i);
                srcDatasourceCmb.addItem(datasource.getAlias());
            }
            srcDatasourceCmb.setSelectedIndex(0);
        }
        srcDatasourceCmb.addItemListener(e -> {
            String selectedDatasourceName = (String) e.getItem();
            Datasource datasource = datasources.get(selectedDatasourceName);
            loadDatasets(srcDatasetCmb, datasource);
        });
        gbc = new GridBagConstraints();
        gbc.fill = GridBagConstraints.BOTH;
        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.gridwidth = 1;
        gbc.gridheight = 1;
        gbc.weightx = 10;
        gbc.weighty = 1;
        gbc.insets = Helper.DefaultInsets;
        srcDataPanel.add(srcDatasourceCmb, gbc);

        JLabel srcDatasetLbl = new JLabel("数据集");
        gbc = new GridBagConstraints();
        gbc.fill = GridBagConstraints.BOTH;
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.gridwidth = 1;
        gbc.gridheight = 1;
        gbc.weightx = 1;
        gbc.weighty = 1;
        gbc.insets = Helper.DefaultInsets;
        srcDataPanel.add(srcDatasetLbl, gbc);

        srcDatasetCmb = new JComboBox<>();
        if (datasourcesCount > 0) {
            String selectedDatasourceName = (String) srcDatasourceCmb.getSelectedItem();
            Datasource datasource = datasources.get(selectedDatasourceName);
            loadDatasets(srcDatasetCmb, datasource);
            srcDatasetCmb.setSelectedIndex(0);
        }
        srcDatasetCmb.addItemListener(e -> {
            String selectedDatasetName = (String) e.getItem();
            if (datasourcesCount > 0) {
                Datasource datasource = datasources.get((String) srcDatasourceCmb.getSelectedItem());
                Datasets datasets = datasource.getDatasets();
                Dataset dataset = datasets.get(selectedDatasetName);
                String availableDatasetName = datasets.getAvailableDatasetName(dataset.getName() + "_Network");
                resultDatasetTxt.setText(availableDatasetName);
            }
        });
        gbc = new GridBagConstraints();
        gbc.fill = GridBagConstraints.BOTH;
        gbc.gridx = 1;
        gbc.gridy = 1;
        gbc.gridwidth = 1;
        gbc.gridheight = 1;
        gbc.weightx = 10;
        gbc.weighty = 1;
        gbc.insets = Helper.DefaultInsets;
        srcDataPanel.add(srcDatasetCmb, gbc);

        JPanel parametersPanel = new JPanel();
        parametersPanel.setBorder(BorderFactory.createTitledBorder("打断设置"));
        parametersPanel.setLayout(new GridBagLayout());
        gbc = new GridBagConstraints();
        gbc.fill = GridBagConstraints.BOTH;
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.gridwidth = 1;
        gbc.gridheight = 1;
        gbc.weightx = 1;
        gbc.weighty = 1;
        gbc.insets = Helper.DefaultInsets;
        centerPanel.add(parametersPanel, gbc);

        autoBreakPointCbx = new JCheckBox("点自动打断线");
        autoBreakPointCbx.setSelected(true);
        gbc = new GridBagConstraints();
        gbc.fill = GridBagConstraints.BOTH;
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.gridwidth = 1;
        gbc.gridheight = 1;
        gbc.weightx = 1;
        gbc.weighty = 1;
        gbc.insets = Helper.DefaultInsets;
        parametersPanel.add(autoBreakPointCbx, gbc);

        autoBreakLineCbx = new JCheckBox("线线自动打断");
        gbc = new GridBagConstraints();
        gbc.fill = GridBagConstraints.BOTH;
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.gridwidth = 1;
        gbc.gridheight = 1;
        gbc.weightx = 1;
        gbc.weighty = 1;
        gbc.insets = Helper.DefaultInsets;
        parametersPanel.add(autoBreakLineCbx, gbc);

        JLabel breakToleranceLbl = new JLabel("打断容限: ");
        gbc = new GridBagConstraints();
        gbc.fill = GridBagConstraints.BOTH;
        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.gridwidth = 1;
        gbc.gridheight = 1;
        gbc.weightx = 1;
        gbc.weighty = 1;
        gbc.insets = Helper.DefaultInsets;
        parametersPanel.add(breakToleranceLbl, gbc);

        breakToleranceTxt = new JTextField();
        breakToleranceTxt.setText("0");
        gbc = new GridBagConstraints();
        gbc.fill = GridBagConstraints.BOTH;
        gbc.gridx = 1;
        gbc.gridy = 2;
        gbc.gridwidth = 1;
        gbc.gridheight = 1;
        gbc.weightx = 10;
        gbc.weighty = 1;
        gbc.insets = Helper.DefaultInsets;
        parametersPanel.add(breakToleranceTxt, gbc);

        JPanel resultPanel = new JPanel();
        resultPanel.setBorder(BorderFactory.createTitledBorder("结果设置"));
        resultPanel.setLayout(new GridBagLayout());
        gbc = new GridBagConstraints();
        gbc.fill = GridBagConstraints.BOTH;
        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.gridwidth = 1;
        gbc.gridheight = 1;
        gbc.weightx = 1;
        gbc.weighty = 1;
        gbc.insets = Helper.DefaultInsets;
        centerPanel.add(resultPanel, gbc);

        JLabel resultDatasourceLbl = new JLabel("数据源: ");
        gbc = new GridBagConstraints();
        gbc.fill = GridBagConstraints.BOTH;
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.gridwidth = 1;
        gbc.gridheight = 1;
        gbc.weightx = 1;
        gbc.weighty = 1;
        gbc.insets = Helper.DefaultInsets;
        resultPanel.add(resultDatasourceLbl, gbc);

        resultDatasourceCmb = new JComboBox<>();
        if (datasourcesCount > 0) {
            for (int i = 0; i < datasourcesCount; i++) {
                Datasource datasource = datasources.get(i);
                resultDatasourceCmb.addItem(datasource.getAlias());
            }
        }
        gbc = new GridBagConstraints();
        gbc.fill = GridBagConstraints.BOTH;
        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.gridwidth = 1;
        gbc.gridheight = 1;
        gbc.weightx = 10;
        gbc.weighty = 1;
        gbc.insets = Helper.DefaultInsets;
        resultPanel.add(resultDatasourceCmb, gbc);

        JLabel resultDatasetLbl = new JLabel("数据集: ");
        gbc = new GridBagConstraints();
        gbc.fill = GridBagConstraints.BOTH;
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.gridwidth = 1;
        gbc.gridheight = 1;
        gbc.weightx = 1;
        gbc.weighty = 1;
        gbc.insets = Helper.DefaultInsets;
        resultPanel.add(resultDatasetLbl, gbc);

        resultDatasetTxt = new JTextField();
        if (datasourcesCount > 0) {
            Datasource datasource = datasources.get((String) srcDatasourceCmb.getSelectedItem());
            String srcDatasetName = (String) srcDatasetCmb.getSelectedItem();
            String availableDatasetName = datasource.getDatasets().getAvailableDatasetName(srcDatasetName + "_Network");
            resultDatasetTxt.setText(availableDatasetName);
        }
        gbc = new GridBagConstraints();
        gbc.fill = GridBagConstraints.BOTH;
        gbc.gridx = 1;
        gbc.gridy = 1;
        gbc.gridwidth = 1;
        gbc.gridheight = 1;
        gbc.weightx = 10;
        gbc.weighty = 1;
        gbc.insets = Helper.DefaultInsets;
        resultPanel.add(resultDatasetTxt, gbc);

        JLabel resultSettingFieldsLbl = new JLabel("结果字段: ");
        gbc = new GridBagConstraints();
        gbc.fill = GridBagConstraints.BOTH;
        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.gridwidth = 1;
        gbc.gridheight = 1;
        gbc.weightx = 1;
        gbc.weighty = 1;
        gbc.insets = Helper.DefaultInsets;
        resultPanel.add(resultSettingFieldsLbl, gbc);

        fieldSettingBtn = new JButton("字段设置");
        fieldSettingBtn.addActionListener(e -> {
            if (datasourcesCount > 0) {
                Datasource datasource = datasources.get((String) srcDatasourceCmb.getSelectedItem());
                if (srcDatasetCmb.getItemCount() > 0) {
                    String selectedDatasetName = (String) srcDatasetCmb.getSelectedItem();
                    DatasetVector datasetVector = (DatasetVector) datasource.getDatasets().get(selectedDatasetName);
                    NetworkFieldsSettingDialog fieldsSettingDialog = new NetworkFieldsSettingDialog((Frame) getOwner(), "字段设置", true, datasetVector.getFieldInfos());
                    fieldsSettingDialog.setVisible(true);
                    lineFieldNames = fieldsSettingDialog.getSelectedFieldNames();
                    fieldsSettingDialog.setVisible(false);
                }
            }
        });
        gbc = new GridBagConstraints();
        gbc.fill = GridBagConstraints.BOTH;
        gbc.gridx = 1;
        gbc.gridy = 2;
        gbc.gridwidth = 1;
        gbc.gridheight = 1;
        gbc.weightx = 1;
        gbc.weighty = 1;
        gbc.insets = Helper.DefaultInsets;
        resultPanel.add(fieldSettingBtn, gbc);
    }

    @Override
    protected void doConfirmCore() {
        setVisible(false);

        Datasources datasources = workspace.getDatasources();
        int datasourcesCount = datasources.getCount();
        if (datasourcesCount > 0) {
            Datasource datasource = datasources.get((String) srcDatasourceCmb.getSelectedItem());
            if (srcDatasetCmb.getItemCount() > 0) {
                String selectedDatasetName = (String) srcDatasetCmb.getSelectedItem();
                DatasetVector datasetVector = (DatasetVector) datasource.getDatasets().get(selectedDatasetName);

                Datasource resultDatasource = datasources.get((String) resultDatasourceCmb.getSelectedItem());

                if (lineFieldNames == null) {
                    FieldInfos fieldInfos = datasetVector.getFieldInfos();
                    int fieldInfosCount = fieldInfos.getCount();
                    StringBuilder stringBuilder = new StringBuilder();
                    for (int i = 0; i < fieldInfosCount; i++) {
                        FieldInfo fieldInfo = fieldInfos.get(i);
                        stringBuilder.append(fieldInfo.getName());
                        stringBuilder.append(AttrConsts.COMMA);
                    }
                    lineFieldNames = stringBuilder.substring(0, stringBuilder.length() - 1).split(AttrConsts.COMMA);
                }

                ProcessBarDialog processBarDialog = new ProcessBarDialog((Frame) getOwner(), "构建二维网络数据", true);
                NetworkBuilder.addSteppedListener(steppedEvent -> {
                    int percent = steppedEvent.getPercent();
                    processBarDialog.setValue(percent);
                    if (percent == 100) {
                        processBarDialog.setVisible(false);
                        JOptionPane.showMessageDialog(this, "二维网络数据构建完成！",
                                "构建二维网络数据", JOptionPane.INFORMATION_MESSAGE);
                    }
                });
                new Thread(() -> NetworkBuilder.buildNetwork(datasetVector, lineFieldNames, resultDatasource, resultDatasetTxt.getText())).start();
                processBarDialog.setVisible(true);
            }
        }
    }
}
